package com.lixw.langchain.controller;

import com.lixw.langchain.enums.LLMEnum;
import dev.langchain4j.model.chat.ChatModel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: ModelTogetherController
 * @Description: 多模型共存
 * @Author: xuweiLi
 * @Create: 2025/8/22 18:46
 **/
@Slf4j
@RestController
public class ModelTogetherController implements ApplicationContextAware {
    @Resource
    private ChatModel qwenChatModel;

    @Resource
    private ChatModel deepseekChatModel;

    private ApplicationContext applicationContext;

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "question",defaultValue = "你是谁？") String question,
                        @RequestParam(value = "code",defaultValue = "qwen-plus") String code) {
        String beanName = LLMEnum.getLLMEnumByCode(code);
        ChatModel chatModel = (ChatModel) applicationContext.getBean(beanName);
        String chat = chatModel.chat(question);
        log.info("大模型返回结果: {}", chat);
        return chat;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}